package com.neuedu.web;

import com.neuedu.pojo.PoorFamily;
import com.neuedu.pojo.CadreTable;
import com.neuedu.pojo.Result;
import com.neuedu.service.PoorFamilyService;
import com.neuedu.service.CadreTableService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/Register")
@Tag(name="注册功能")
public class RegisterController {
    @Resource
    private PoorFamilyService poorFamilyService;
    @Resource
    private CadreTableService cadreTableService;
    //注册功能
    @GetMapping()
    public Result<Void> RegisterUser(@RequestParam(name = "username",defaultValue = "")String number,
                                     @RequestParam(name ="userRealname",defaultValue = "")String name,
                                     @RequestParam(name ="password",defaultValue = "")String password,
                                     @RequestParam(name ="role",defaultValue = "")String role,
                                     @RequestParam(name="idCard",defaultValue = "")String idCard,
                                     @RequestParam(name = "gender",defaultValue = "")String gender,
                                     @RequestParam(name="confirmPassword",defaultValue = "")String confirmPassword,
                                     @RequestParam(name = "account",defaultValue = "")String phone) {
        char firstChar = 0;
        if (number != null && number.length() > 0) {
            firstChar = number.charAt(0);
        } else {
            return Result.fail();
        }
        if (firstChar == 'p' || firstChar == 'P') {
            PoorFamily poorFamily = new PoorFamily();
            poorFamily.setFamilyNumber(number);
            poorFamily.setHeadName(name);
            poorFamily.setPassword(password);
            poorFamily.setIdCard(idCard);
            poorFamily.setGender(gender);
            poorFamily.setPhone(phone);
            return poorFamilyService.addPoorFamily(poorFamily);
        } else if (firstChar == 'c' || firstChar == 'C') {
            CadreTable cadreTable = new CadreTable();
            cadreTable.setCadreNumber(number);
            cadreTable.setName(name);
            cadreTable.setPassword(password);
            cadreTable.setRoleType(role);
            cadreTable.setIdCard(idCard);
            cadreTable.setGender(gender);
            cadreTable.setPhone(phone);
            return cadreTableService.addCadreTable(cadreTable);
        } else {
            return Result.fail();
        }

    }


}
